﻿using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace ZHDJ.MobileInterface
{
    public sealed class SerializeHelper
    {
        public static string JsonSerializer<T>(T objModel)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(stream, objModel);
                return Encoding.UTF8.GetString(stream.ToArray());
            }
        }

        public static T JsonDeserialize<T>(string jsonString)
        {
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                return (T)serializer.ReadObject(stream);
            }
        }
    }
}